package com.health.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送
 */
public class SMSUtil {

    private static final String accessKeyId = "LTAI4GCutVEvCEveJZteTYn1";
    private static final String accessSecret = "iSWM64RJaFs4FMwUc8mZ0aDJKoBQbm";
    private static final String SignName = "学涯";
    private static final String TemplateCode = "SMS_211497389";

    /**
     * 发送短信验证码
     */
    public static boolean send(String phone, int code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SignName);
        request.putQueryParameter("TemplateCode", TemplateCode);
        request.putQueryParameter("TemplateParam", "{'code':'" + code + "'}");
        CommonResponse response = client.getCommonResponse(request);

        //解析响应的数据
        String result = JSON.parseObject(response.getData()).getString("Code");
        return result != null && result.equals("OK");
    }
}